﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类article_content。
    /// </summary>
    public class article_content
    {
        public article_content()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int News_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_article_content where News_id=" + News_id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.article_content model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_article_content(");
            strSql.Append("Title,Dat,author,Source,Content,Classid,Tj,sh,Hit,TitlePic,Keyword,Link,All_wjm,All_wjs,One_wjm,faburen,shr,focus,pid,fb_jg,fb_wh,ss_dat");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.Title + "',");
            strSql.Append("'" + model.Dat + "',");
            strSql.Append("'" + model.author + "',");
            strSql.Append("'" + model.Source + "',");
            strSql.Append("'" + model.Content + "',");
            strSql.Append("" + model.Classid + ",");
            strSql.Append("" + model.Tj + ",");
            strSql.Append("" + model.sh + ",");
            strSql.Append("" + model.Hit + ",");
            strSql.Append("'" + model.TitlePic + "',");
            strSql.Append("'" + model.Keyword + "',");
            strSql.Append("'" + model.Link + "',");
            strSql.Append("'" + model.All_wjm + "',");
            strSql.Append("" + model.All_wjs + ",");
            strSql.Append("'" + model.One_wjm + "',");
            strSql.Append("'" + model.faburen + "',");
            strSql.Append("'" + model.shr + "',");
            strSql.Append("" + model.focus + ",");
            strSql.Append("" + model.pid + ",");
            strSql.Append("'" + model.fb_jg + "',");
            strSql.Append("'" + model.fb_wh + "',");
            strSql.Append("'" + model.ss_dat + "'");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingleNews(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.article_content model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_article_content set ");
            if (model.Title != null)
                strSql.Append("Title='" + model.Title + "',");
            if (model.Dat != null)
                strSql.Append("Dat='" + model.Dat + "',");
            if (model.author != null)
                strSql.Append("author='" + model.author + "',");
            if (model.Source != null)
                strSql.Append("Source='" + model.Source + "',");
            if (model.Content != null)
                strSql.Append("Content='" + model.Content + "',");
            if (model.Classid.Equals(Int32.Parse("0")) != true)
                strSql.Append("Classid=" + model.Classid + ",");
            if (model.Tj != null)
                strSql.Append("Tj=" + model.Tj + ",");
            if (model.sh != null)
                strSql.Append("sh=" + model.sh + ",");
            if (model.Hit.Equals(Int32.Parse("0")) != true)
                strSql.Append("Hit=" + model.Hit + ",");
            if (model.TitlePic != null)
                strSql.Append("TitlePic='" + model.TitlePic + "',");
            if (model.Keyword != null)
                strSql.Append("Keyword='" + model.Keyword + "',");
            if (model.Link != null)
                strSql.Append("Link='" + model.Link + "',");
            if (model.All_wjm != null)
                strSql.Append("All_wjm='" + model.All_wjm + "',");
            if (model.All_wjs.Equals(Int32.Parse("0")) != true)
                strSql.Append("All_wjs=" + model.All_wjs + ",");
            if (model.One_wjm != null)
                strSql.Append("One_wjm='" + model.One_wjm + "',");
            if (model.faburen != null)
                strSql.Append("faburen='" + model.faburen + "',");
            if (model.shr != null)
                strSql.Append("shr='" + model.shr + "',");
            if (model.pid.Equals(Int32.Parse("0")) != true)
                strSql.Append("pid=" + model.pid + ",");
            if (model.fb_jg != null)
                strSql.Append("fb_jg='" + model.fb_jg + "',");
            if (model.fb_wh != null)
                strSql.Append("fb_wh='" + model.fb_wh + "',");
            if (model.ss_dat != null)
                strSql.Append("ss_dat='" + model.ss_dat + "',");
            strSql.Append(" focus=focus ");
            strSql.Append(" where News_id=" + model.News_id + "");
            if (DataObject.ExecuteSqlNews(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改点击量
        /// </summary>
        /// <param name="id">新闻ID</param>
        /// <returns></returns>
        public bool UpdateNewsHit(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_content set hit=hit+1 ");
            strSql.Append("where news_id= " + id.ToString());
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        public bool UpdateLink(int id, string link)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_content set link='" + link + "' ");
            strSql.Append(" where news_id= " + id.ToString());
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改管理用户发布量
        /// </summary>
        /// <param name="user">管理用户</param>
        /// <returns></returns>
        public bool UpdateManFab(string user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_user set fb_num=fb_num+1 ");
            if (user.Trim() != "")
                strSql.Append(" where username='" + user + "' ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改管理用户审核量
        /// </summary>
        /// <param name="user">管理用户</param>
        /// <returns></returns>
        public bool UpdateManSh(string user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_user set sh_num=sh_num+1 ");
            if (user.Trim() != "")
                strSql.Append(" where username='" + user + "' ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改管理用户审核量
        /// </summary>
        /// <param name="user">管理用户</param>
        /// <param name="num">新加审核量</param>
        /// <returns></returns>
        public bool UpdateManSh(string user, int num)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_user set sh_num= sh_num+" + num.ToString());
            if (user.Trim() != "")
                strSql.Append(" where username='" + user + "' ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改审核推荐
        /// </summary>
        /// <param name="shval">审核值</param>
        /// <param name="tjval">推荐值</param>
        /// <param name="ids">新闻ID</param>
        /// <returns></returns>
        public bool UpdateShTj(string shval, string tjval, string ids, string user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_content set ");
            if (shval.Trim() != "")
            {
                strSql.Append("sh=" + shval + ",shr='" + user + "',");
            }
            if (tjval.Trim() != "")
                strSql.Append("tj=" + tjval + ",");
            strSql.Append(" focus=focus ");
            strSql.Append(" where News_id in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public bool UpdateClassId(string ids, string classid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_content set ");
            strSql.Append("classid=" + classid);
            strSql.Append(" where news_id in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改时间
        /// </summary>
        /// <param name="time">时间</param>
        /// <param name="ids">新闻ID</param>
        /// <returns></returns>
        public bool UpdateTime(string time, string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_article_content set dat='" + time + "' ");
            strSql.Append(" where News_id in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int News_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_content ");
            strSql.Append(" where News_id=" + News_id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="classid">新闻ID</param>
        /// <returns></returns>
        public bool Delete(string newsid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_content ");
            strSql.Append(" where News_id in (" + newsid + ") ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="classid">新闻类别</param>
        /// <returns></returns>
        public bool DeleteClassNews(string classid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_content ");
            strSql.Append(" where classid in (" + classid + ") ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.article_content GetModel(int News_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [News_id],[Title],[Dat],[author],[Source],[Content],[Classid],[Tj],[sh],[Hit],[TitlePic],[Keyword],[Link],[All_wjm],[All_wjs],[One_wjm],[faburen],[shr],[focus],[pid],[fb_jg],[fb_wh],[ss_dat] ");
            strSql.Append(" from lt_article_content ");
            strSql.Append(" where News_id=" + News_id);
            ltModel.Model.article_content model = new ltModel.Model.article_content();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.News_id = News_id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.Dat = ds.Tables[0].Rows[0]["Dat"].ToString();
                model.author = ds.Tables[0].Rows[0]["author"].ToString();
                model.Source = ds.Tables[0].Rows[0]["Source"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["Classid"].ToString() != "")
                {
                    model.Classid = int.Parse(ds.Tables[0].Rows[0]["Classid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Tj"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Tj"].ToString() == "1") || (ds.Tables[0].Rows[0]["Tj"].ToString().ToLower() == "true"))
                    {
                        model.Tj = "1";
                    }
                    else
                    {
                        model.Tj = "0";
                    }
                }

                if (ds.Tables[0].Rows[0]["sh"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["sh"].ToString() == "1") || (ds.Tables[0].Rows[0]["sh"].ToString().ToLower() == "true"))
                    {
                        model.sh = "1";
                    }
                    else
                    {
                        model.sh = "0";
                    }
                }

                if (ds.Tables[0].Rows[0]["Hit"].ToString() != "")
                {
                    model.Hit = int.Parse(ds.Tables[0].Rows[0]["Hit"].ToString());
                }
                model.TitlePic = ds.Tables[0].Rows[0]["TitlePic"].ToString();
                model.Keyword = ds.Tables[0].Rows[0]["Keyword"].ToString();
                model.Link = ds.Tables[0].Rows[0]["Link"].ToString();
                model.All_wjm = ds.Tables[0].Rows[0]["All_wjm"].ToString();
                if (ds.Tables[0].Rows[0]["All_wjs"].ToString() != "")
                {
                    model.All_wjs = int.Parse(ds.Tables[0].Rows[0]["All_wjs"].ToString());
                }
                model.One_wjm = ds.Tables[0].Rows[0]["One_wjm"].ToString();
                model.faburen = ds.Tables[0].Rows[0]["faburen"].ToString();
                model.shr = ds.Tables[0].Rows[0]["shr"].ToString();
                if (ds.Tables[0].Rows[0]["focus"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["focus"].ToString() == "1") || (ds.Tables[0].Rows[0]["focus"].ToString().ToLower() == "true"))
                    {
                        model.focus = "1";
                    }
                    else
                    {
                        model.focus = "0";
                    }
                }

                if (ds.Tables[0].Rows[0]["pid"].ToString() != "")
                {
                    model.pid = int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
                }
                model.fb_jg = ds.Tables[0].Rows[0]["fb_jg"].ToString();
                model.fb_wh = ds.Tables[0].Rows[0]["fb_wh"].ToString();
                model.ss_dat = ds.Tables[0].Rows[0]["ss_dat"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [News_id],[Title],[Dat],[author],[Source],[Content],[Classid],[Tj],[sh],[Hit],[TitlePic],[Keyword],[Link],[All_wjm],[All_wjs],[One_wjm],[faburen],[shr],[focus],[pid],[fb_jg],[fb_wh],[ss_dat] ");
            strSql.Append(" FROM lt_article_content ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string topNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (PageValidate.IsNumber(topNum))
                strSql.Append(" top " + topNum + " ");
            strSql.Append(" [News_id],[Title],[Dat],[author],[Source],[Content],[Classid],[Tj],[sh],[Hit],[TitlePic],[Keyword],[Link],[All_wjm],[All_wjs],[One_wjm],[faburen],[shr],[focus],[pid],[fb_jg],[fb_wh],[ss_dat] ");
            strSql.Append(" FROM lt_article_content ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 查询新闻(指定多少条记录)
        /// </summary>
        /// <param name="cids">新闻类别(集合)</param>
        /// <param name="count">新闻条数</param>
        /// <returns></returns>
        public DataSet FindShowNews(string cids, int count)
        {
            string sql = "";
            string ctstr = "";
            if (count > 0)
                ctstr = " TOP " + count + " ";
            if (cids.IndexOf(",") > 0)
            {
                string si = "";
                string[] cs = cids.Split(new char[] { ',' });
                for (int i = 0; i < cs.Length; i++)
                {
                    si = si + "All_wjm like '%|" + cs[i].ToString() + "|%' or ";
                }
                sql = "SELECT " + ctstr + " * FROM lt_article_content where ( " + si + "  Classid in (" + cids + ") ) and sh=1 and dat<getdate() order by dat desc,news_id desc";
            }
            else
            {
                sql = "SELECT " + ctstr + " * FROM lt_article_content where ( All_wjm like '%|" + cids + "|%' or Classid in (" + cids + ") ) and sh=1 and dat<getdate() order by dat desc,news_id desc";
            }

            return DataObject.Query(sql);
        }

        /// <summary>
        /// 查询标题图片
        /// </summary>
        /// <param name="cids">新闻类别ID</param>
        /// <param name="count">新闻条数</param>
        /// <returns></returns>
        public DataSet FindPicNews(string cids, int count)
        {
            string sql = "";
            string ctstr = "";
            if (count > 0)
                ctstr = " TOP " + count + " ";
            if (cids.IndexOf(",") > 0)
            {
                string si = "";
                string[] cs = cids.Split(new char[] { ',' });
                for (int i = 0; i < cs.Length; i++)
                {
                    si = si + "All_wjm like '%|" + cs[i].ToString() + "|%' or ";
                }
                sql = "SELECT " + ctstr + " * FROM lt_article_content where ( " + si + "  Classid in (" + cids + ") ) and ltrim(titlepic)<>'' and sh=1 and dat<getdate() order by dat desc,news_id desc";
            }
            else
            {
                sql = "SELECT " + ctstr + " * FROM lt_article_content where ( All_wjm like '%|" + cids + "|%' or Classid in (" + cids + ") ) and ltrim(titlepic)<>'' and sh=1 and dat<getdate() order by dat desc,news_id desc";
            }

            return DataObject.Query(sql);
        }

        /// <summary>
        /// 查询上条新闻
        /// </summary>
        /// <param name="newsCid">新闻类别</param>
        /// <param name="id">当前新闻ID</param>
        /// <returns></returns>
        public DataTable FindBackNews(string newsCid, string id)
        {
            string sql = "select top 1 news_id,title,dat from lt_article_content where sh=1 and classid in(" + newsCid + ") and news_id>" + id + " and dat<getdate() order by news_id asc";
            return DataObject.Query(sql).Tables[0];
        }

        /// <summary>
        /// 查询下条新闻
        /// </summary>
        /// <param name="newsCid">新闻类别</param>
        /// <param name="id">当前新闻ID</param>
        /// <returns></returns>
        public DataTable FindNexNews(string newsCid, string id)
        {
            string sql = "select top 1 news_id,title,dat from lt_article_content where sh=1 and classid in(" + newsCid + ") and news_id<" + id + " and dat<getdate() order by news_id desc";
            return DataObject.Query(sql).Tables[0];
        }

        /// <summary>
        /// 查询相关新闻
        /// </summary>
        /// <param name="kw">关键字</param>
        /// <param name="id">当前新闻ID</param>
        /// <returns></returns>
        public DataTable FindKWNews(string kw, string id)
        {
            string sql = "";
            string whe = "";
            string[] str = kw.Split(new Char[] { ' ' });
            for (int k = 0; k < str.Length; k++)
            {
                if (str[k].Trim() != "")
                {
                    whe = whe + " keyword like '%" + str[k].Trim() + "%' or ";
                }
            }
            if (whe.Length > 3)
                whe = "(" + whe.Substring(0, whe.Length - 3) + ")";
            if (whe.Length > 0)
                sql = "select top 5 * from lt_article_content where sh=1 and " + whe + " and news_id<>" + id + " and dat<getdate() order by dat desc";
            else
                sql = "select top 5 * from lt_article_content where sh=1 and keyword='000000' order by dat desc";
            return DataObject.Query(sql).Tables[0];
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "article_content";
            parameters[1].Value = "News_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DataObject.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}